<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="120px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="标题" prop="title">
						<el-input type="textarea" v-model="form.title" placeholder="请输入标题" />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="内容" prop="content">
						<Editor v-model:getHtml="form.content" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="状态" prop="status">
						<el-select v-model="form.status" placeholder="请选择状态">
							<el-option :value="item.value" :label="item.label" v-for="(item, index) in notify_status" :key="index"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="资讯类型" prop="typeId">
						<el-select v-model="form.typeId" placeholder="请选择资讯类型">
							<el-option :value="item.value" :label="item.label" v-for="(item, index) in society_type" :key="index"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="永久有效" prop="immortal">
						<el-select v-model="form.immortal" placeholder="请选择永久有效状态" @change="immortalChange">
							<el-option :value="item.value" :label="item.label" v-for="(item, index) in valid_flag" :key="index"></el-option>
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="截止日期" prop="deadline">
						<el-date-picker
							:disabled="form.immortal == 'yes'"
							type="datetime"
							placeholder="请选择截止日期"
							v-model="form.deadline"
							value-format="YYYY-MM-DD HH:mm:ss"
						></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="notifyAnnouncementSocietyDialog">
import { useMessage } from '@/hooks/message';
import { getObj, addObj, putObj } from '@/api/oa/notifyAnnouncementSociety';
import { list as roleList } from '@/api/admin/role';
import { deptTree } from '@/api/admin/dept';
import { useDict } from '@/hooks/dict';

const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
const deptData = ref<any[]>([]);
const roleData = ref<any[]>([]);

// 定义字典
const { notify_status, valid_flag, society_type } = useDict('notify_status', 'valid_flag', 'society_type');
// 提交表单数据
const form = reactive({
	id: '',
	title: '',
	content: '',
	status: '',
	immortal: '',
	deadline: '',
});

// 定义校验规则
const dataRules = ref({
	title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
	content: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
	status: [{ required: true, message: '状态不能为空', trigger: 'blur' }],
	immortal: [{ required: true, message: '永久有效的不能为空', trigger: 'blur' }],
	deadline: [{ required: false, message: '截止日期不能为空', trigger: 'blur' }],
});

// 初始化部门数据
const getDeptData = () => {
	// 获取部门数据
	deptTree().then((res) => {
		deptData.value = res.data;
	});
};

// 角色数据
const getRoleData = () => {
	roleList().then((res) => {
		roleData.value = res.data;
	});
};

// 打开弹窗
const openDialog = (id: string) => {
	// 加载使用的数据
	getDeptData();
	getRoleData();
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取notifyAnnouncementSociety信息
	if (id) {
		form.id = id;
		getnotifyAnnouncementSocietyData(id);
	}
};

const immortalChange = (e: any) => {
	if (e == 'yes') {
		dataRules.value.deadline[0].required = false;
	} else {
		dataRules.value.deadline[0].required = true;
	}
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getnotifyAnnouncementSocietyData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
